/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function Login(){


    Ext.apply(Ext.form.VTypes, {

        password : function(val, field) {
            if (field.senha) {
                var pwd = Ext.getCmp(field.senha);
                return (4 == pwd.size);
            }
            return true;
        },

        passwordText : 'Senha inválida!'
    });

   
    var formulario = new Ext.FormPanel({
        labelWidth: 75, // label settings here cascade unless overridden
        frame:true,
        id:'form',
        bodyStyle:'padding:5px 5px 0',
        width: 350,
        defaults: {
            width: 230
        },
        defaultType: 'textfield',
        renderTo:'login',
        items: [{
            xtype: 'textfield',
            fieldLabel: '<html><img src="images/usuario.png" align="left" />&nbsp;&nbsp;&nbsp; Login</html>',
            id:'campoLogin',
            name: 'login',
            autoCreate: {
                    tag: "input",
                    type: "text",
                    autocomplete: "off",
                    maxlength: 15
                },
            icon:'images/usuario.png',
            allowBlank:false
        },{
           
            inputType:'password',
            fieldLabel: '<html><img src="images/key.png" align="left" />&nbsp;&nbsp;&nbsp; Senha</html>',
            id:'campoSenha',
            name: 'senha',
            autoCreate: {
                    tag: "input",
                    type: "text",
                    autocomplete: "off",
                    maxlength: 15
                },
            icon:'images/key.png',
            allowBlank:false
        }
        ],

        buttons: [{
            text: '<html><img src="images/entrar_programa.png" />&nbsp;&nbsp;&nbsp; Entrar </html>',
            //icon:'images/entrar_programa.png',
            handler: function(){
                     
                var myMask = new Ext.LoadMask(Ext.getBody(), {
                    msg:"Verificando informações...",
                    timeout:100
                });

                myMask.enable();
                myMask.show();

              

                Ext.Ajax.request({
                    url: 'login.do',
                    autoAbort : true,
                    isLoadin: true,
                    params: {
                        login: Ext.getCmp('campoLogin').getValue() ,
                        senha:  Ext.getCmp('campoSenha').getValue()
                    },
                    success: function(result, request){

                        if(result.responseText == '{success:true}'){
                            myMask.hide();
                            // será redirecionado
                            window.location = "index.jsp"

                        }
                        if(result.responseText == '{success:false}'){
                            myMask.hide();

                            Ext.Msg.show({
                                title: 'Atenção',
                                msg: 'Login e senha não conferem!',
                                icon: Ext.Msg.ERROR,
                                buttons: Ext.Msg.OK,
                                animEl:'form',
                                modal: true
                            });
                        }
                    },failure:function(){

                    }
                })
              
            }

        }]
    });

   var cadastroPacienteWindow = new Ext.Window({
    title:         'Autenticação',
    resizable :    false,
    closable: false,
    buttonAlign:   'center',
    items: formulario
   });
   cadastroPacienteWindow.show();
}

